<?php
	!defined('DYHB_PATH') && exit;
	class ArticleController extends InitController{
		/*发表文章页面*/
		public function add(){
			if($GLOBALS['___login___']==false){
				$this->assign('__JumpUrl__',Dyhb::U('index/index'));
				$this->E('你还没有登陆');
			}
			$this->assign('title','添加文章');
			$this->display();
		}
		/*文章发表管理*/
		public function add_deal(){
			if($GLOBALS['___login___']==false){
				$this->assign('__JumpUrl__',Dyhb::U('index/index'));
				$this->E('你还没有登陆');
			}
			$oArticle=new ArticleModel();
			$oArticle->article_author=$GLOBALS['___login___']['regname'];
			$oArticle->article_createdate=date('Y-m-d H:m:s');
			$oArticle->article_content=G::getGpc('article_contents','p');
			$oArticle->save();
			if(!$oArticle->isError()){
				$this->assign('__JumpUrl__',Dyhb::U('index/index'));
				$this->S('文章发表成功');
			}else{
				$this->E($oArticle->getErrorMessage());
			}
		}
		/*显示x自己文章列表*/
		public function xlist(){
			if($GLOBALS['___login___']==false){
				$this->assign('__JumpUrl__',Dyhb::U('index/index'));
				$this->E('你还没有登陆');
			}
			$arr=array('article_author'=>$GLOBALS['___login___']['regname']);
			$currentPage=G::getGpc('page','g');
			if(empty($currentPage)){
			 $currentPage=1;
			}
			$Nums=ArticleModel::F($arr)->all()->getCounts();
			$pageSize=$GLOBALS['___config___']['site_lognum'];
			$subNums=4;
			$subLink=Dyhb::U('article/xlist?page=');
			$subType=2;
			$oPage=new myPage($Nums,$pageSize,$currentPage,$subNums,$subLink,$subType);

			$start=($currentPage-1)*$pageSize;
			if($pageSize>$Nums){
				$pageSize=$Nums;
			}
			$arr=ArticleModel::F($arr)->order('article_id DESC')->limit($start,$pageSize)->getAll();
			$this->assign('oPage',$oPage);
			$this->assign('article_list',$arr);
			$this->assign('title','博客文章');
			$this->display();
		}
		/*显示所有文章*/
		public function allList(){
			
			$currentPage=G::getGpc('page','g');
			if(empty($currentPage)){
			 $currentPage=1;
			}
			$Nums=ArticleModel::F()->all()->getCounts();
			$pageSize=$GLOBALS['___config___']['site_lognum'];
			$subNums=4;
			$subLink=Dyhb::U('article/allList?page=');
			$subType=2;
			$oPage=new myPage($Nums,$pageSize,$currentPage,$subNums,$subLink,$subType);

			$start=($currentPage-1)*$pageSize;
			if($pageSize>$Nums){
				$pageSize=$Nums;
			}
			$arr=ArticleModel::F()->all()->order('article_id DESC')->limit($start,$pageSize)->query();
			$this->assign('article_allList',$arr);
			$this->assign('oPage',$oPage);
			$this->assign('title','博客文章');
			$this->display();
		}
		/*显示文章及相关评论*/
		public function show(){
			$id=G::getGpc('id','g');
			$oArt=ArticleModel::F('article_id=?',$id)->getOne();
			$oArt->article_scans=$oArt->article_scans+1;
			$oArt->save();
			$oCom=FilecommentModel::F('article_id=?',$id)->getAll();
			//G::dump($oCom);
			$this->assign('oArt',$oArt);
			$this->assign('oCom',$oCom);
			$this->assign('title','浏览文章');
			$this->display();
		}
		/*删除文章及删除该文章的评论*/
		public function deleteArt(){
			$id=G::getGpc('id','g');
			$oArt=ArticleModel::F('article_id=?',$id)->getOne();
			if($GLOBALS['___login___']['regname']!=$oArt->article_author){
				$this->E("你不能删除他人的文章");
			}
			$oArt->destroy();
			$oCom=FilecommentModel::F('article_id=?',$id)->getAll();
			foreach($oCom as $oComment){
				$oComment->destroy();
			}
			if(!$oArt->isError()){
				$this->S("文章删除成功");
			}else{
				$this->E("文章删除失败");
			}
		}
	}
?>